home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / sys5unix.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  9KB  |  522 lines

  1. /*
  2.     FILE: unix.c
  3.  
  4.     Routines: This file contains the following routines:
  5.         fileinit()
  6.         eihalt()
  7.         kbread()
  8.         clksec()
  9.         tmpfile()
  10.         restore()
  11.         stxrdy()
  12.         disable()
  13.         memstat()
  14.         filedir()
  15.         sysreset()
  16.  
  17.     Written by Mikel Matthews, N9DVG
  18.     SYS5 stuff added by Jere Sandidge, K4FUM
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <termio.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/times.h>
  27. #include <time.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #undef    toupper
  31. #undef    tolower
  32.  
  33. #include "global.h"
  34. #include "config.h"
  35. #include "cmdparse.h"
  36. #include "iface.h"
  37. #include "unix.h"
  38. #include "ndir.h"
  39.  
  40. #define    MAXCMD    1024
  41.  
  42. int asy_attach();
  43. #ifdef    SUNOS4
  44. char *sprintf();
  45. #endif
  46.  
  47. extern struct cmds attab[];
  48. extern struct termio savecon;
  49.  
  50. unsigned nasy;
  51.  
  52. fileinit(argv0)
  53. char *argv0;
  54. {
  55.     int el;
  56.     char *ep, *cp, *malloc(), *getenv(), *getcwd();
  57.     char tmp[MAXCMD];
  58.     extern char *startup, *config, *userfile, *hosts, *mailspool;
  59.     extern char *mailqdir, *mailqueue, *routeqdir, *alias, *netexe;
  60. #ifdef    _FINGER
  61.     extern char *fingerpath;
  62. #endif
  63. #ifdef    XOBBS
  64.     extern char *bbsexe;
  65. #endif
  66.  
  67.     /* Get the name of the currently executing program */
  68.     if ((cp = malloc((unsigned)(strlen(argv0) + 1))) == NULL)
  69.         perror("malloc");
  70.     else {
  71.         sprintf(cp, "%s", argv0);
  72.         netexe = cp;
  73.     }
  74.  
  75. #ifdef    XOBBS
  76.     /* Get the path to the W2XO BBS executable. */
  77.     if ((ep = getenv("XOBBS")) == NULLCHAR) {
  78.         bbsexe = "xobbs";
  79.     } else {
  80.         if ((cp = malloc((unsigned)(strlen(ep) + 2))) == NULL)
  81.             perror("malloc");
  82.         else {
  83.             sprintf(cp, "%s", ep);
  84.             bbsexe = cp;
  85.         }
  86.     }
  87. #endif
  88.     /* Try to get home directory name */
  89.     if ((ep = getenv("NETHOME")) == NULLCHAR) {
  90.         if ((ep = getenv("HOME")) == NULLCHAR) {
  91.             ep = ".";
  92.         }
  93.     }
  94.     el = strlen(ep);
  95.     /* Replace each of the file name strings with the complete path */
  96.     if (*startup != '/') {
  97.         if ((cp = malloc((unsigned)(el + strlen(startup) + 2))) == NULL)
  98.             perror("malloc");
  99.         else {
  100.             sprintf(cp, "%s/%s", ep, startup);
  101.             startup = cp;
  102.         }
  103.     }
  104.  
  105.     if (*config != '/') {
  106.         if ((cp = malloc((unsigned)(el + strlen(config) + 2))) == NULL)
  107.             perror("malloc");
  108.         else {
  109.             sprintf(cp, "%s/%s", ep, config);
  110.             config = cp;
  111.         }
  112.     }
  113.  
  114.     if (*userfile != '/') {
  115.         if ((cp = malloc((unsigned)(el + strlen(userfile) + 2))) == NULL)
  116.             perror("malloc");
  117.         else {
  118.             sprintf(cp, "%s/%s", ep, userfile);
  119.             userfile = cp;
  120.         }
  121.     }
  122.  
  123.     if (*hosts != '/') {
  124.         if ((cp = malloc((unsigned)(el + strlen(hosts) + 2))) == NULL)
  125.             perror("malloc");
  126.         else {
  127.             sprintf(cp, "%s/%s", ep, hosts);
  128.             hosts = cp;
  129.         }
  130.     }
  131.  
  132.     if (*alias != '/') {
  133.         if ((cp = malloc((unsigned)(el + strlen(alias) + 2))) == NULL)
  134.             perror("malloc");
  135.         else {
  136.             sprintf(cp, "%s/%s", ep, alias);
  137.             alias = cp;
  138.         }
  139.     }
  140.  
  141. #ifdef        _FINGER
  142.     if (*fingerpath != '/') {
  143.         if ((cp = malloc((unsigned)(el + strlen(fingerpath) + 2))) == NULL)
  144.             perror("malloc");
  145.         else {
  146.             sprintf(cp, "%s/%s", ep, fingerpath);
  147.             fingerpath = cp;
  148.         }
  149.     }
  150. #endif
  151.  
  152.     /* Try to get home directory name */
  153.     if ((ep = getenv("NETSPOOL")) == NULLCHAR)
  154.         ep = "/usr/spool";
  155.     el = strlen(ep);
  156.  
  157.     if (*mailspool != '/') {
  158.         if ((cp = malloc((unsigned)(el + strlen(mailspool) + 2))) == NULL)
  159.             perror("malloc");
  160.         else {
  161.             sprintf(cp, "%s/%s", ep, mailspool);
  162.             mailspool = cp;
  163.         }
  164.     }
  165.  
  166.     if (*mailqdir != '/') {
  167.         if ((cp = malloc((unsigned)(el + strlen(mailqdir) + 2))) == NULL)
  168.             perror("malloc");
  169.         else {
  170.             sprintf(cp, "%s/%s", ep, mailqdir);
  171.             mailqdir = cp;
  172.         }
  173.     }
  174.  
  175.     if (*mailqueue != '/') {
  176.         if ((cp = malloc((unsigned)(el + strlen(mailqueue) + 2))) == NULL)
  177.             perror("malloc");
  178.         else {
  179.             sprintf(cp, "%s/%s", ep, mailqueue);
  180.             mailqueue = cp;
  181.         }
  182.     }
  183.  
  184.     if (*routeqdir != '/') {
  185.         if ((cp = malloc((unsigned)(el + strlen(routeqdir) + 2))) == NULL)
  186.             perror("malloc");
  187.         else {
  188.             sprintf(cp, "%s/%s", ep, routeqdir);
  189.             routeqdir = cp;
  190.         }
  191.     }
  192. }
  193.  
  194. /* action routine for remote reset */
  195. sysreset()
  196. {
  197.     extern char *netexe;
  198.  
  199.     execlp(netexe,netexe,0);
  200.     execlp("net","net",0);
  201.     printf("reset failed: exiting\n");
  202.     exit(1);
  203. }
  204.  
  205. eihalt()
  206. {
  207.     void tnix_scan();
  208.  
  209.     tnix_scan();
  210. }
  211.  
  212.  
  213. kbread()
  214. {
  215.     unsigned char c;
  216.  
  217.     if (read(fileno(stdin), &c, 1) <= 0)
  218.         return -1;
  219.  
  220.     return ((int) c);
  221. }
  222.  
  223.  
  224. clksec()
  225. {
  226.     time_t tim, time();
  227.  
  228.     (void) time(&tim);
  229.  
  230.     return ((int) tim);
  231. }
  232.  
  233.  
  234. /*ARGSUSED*/
  235. restore(state)
  236. char state;
  237. {
  238. }
  239.  
  240.  
  241. /*ARGSUSED*/
  242. stxrdy(dev)
  243. int16 dev;
  244. {
  245.     return 1;
  246. }
  247.  
  248.  
  249. disable()
  250. {
  251. }
  252.  
  253.  
  254. memstat()
  255. {
  256.     return 0;
  257. }
  258.  
  259.  
  260. /* wildcard filename lookup */
  261. filedir(name, times, ret_str)
  262. char    *name;
  263. int    times;
  264. char    *ret_str;
  265. {
  266.     static char    dname[128], fname[128];
  267.     static DIR *dirp = NULL;
  268.     struct direct *dp;
  269.     struct stat sbuf;
  270.     char    *cp, temp[128];
  271.  
  272.     /*
  273.      * Make sure that the NULL is there in case we don't find anything
  274.      */
  275.     ret_str[0] = '\0';
  276.  
  277.     if (times == 0) {
  278.         /* default a null name to *.* */
  279.         if (name == NULL || *name == '\0')
  280.             name = "*.*";
  281.         /* split path into directory and filename */
  282.         if ((cp = strrchr(name, '/')) == NULL) {
  283.             strcpy(dname, ".");
  284.             strcpy(fname, name);
  285.         } else {
  286.             strcpy(dname, name);
  287.             dname[cp - name] = '\0';
  288.             strcpy(fname, cp + 1);
  289.             /* root directory */
  290.             if (dname[0] == '\0')
  291.                 strcpy(dname, "/");
  292.             /* trailing '/' */
  293.             if (fname[0] == '\0')
  294.                 strcpy(fname, "*.*");
  295.         }
  296.         /* close directory left over from another call */
  297.         if (dirp != NULL)
  298.             closedir(dirp);
  299.         /* open directory */
  300.         if ((dirp = opendir(dname)) == NULL) {
  301.             printf("Could not open DIR (%s)\n", dname);
  302.             return;
  303.         }
  304.     } else {
  305.         /* for people who don't check return values */
  306.         if (dirp == NULL)
  307.             return;
  308.     }
  309.  
  310.     /* scan directory */
  311.     while ((dp = readdir(dirp)) != NULL) {
  312.         /* test for name match */
  313.         if (wildmat(dp->d_name, fname)) {
  314.             /* test for regular file */
  315.             sprintf(temp, "%s/%s", dname, dp->d_name);
  316.             if (stat(temp, &sbuf) < 0)
  317.                 continue;
  318.             if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  319.                 continue;
  320.             strcpy(ret_str, dp->d_name);
  321.             break;
  322.         }
  323.     }
  324.  
  325.     /* close directory if we hit the end */
  326.     if (dp == NULL) {
  327.         closedir(dirp);
  328.         dirp = NULL;
  329.     }
  330. }
  331.  
  332.  
  333. /* checks the time then ticks and updates ISS */
  334. void
  335. check_time()
  336. {
  337.     int32 iss();
  338.     long times();
  339.  
  340.     struct tms tb;
  341.     static long clkval;
  342.     long ntime, offset;
  343.  
  344.     /* read elapsed real time (typ. 60 Hz) */
  345.     ntime = times(&tb);
  346.  
  347.     /* resynchronize if the error is large (10 seconds or more) */
  348.     offset = ntime - clkval;
  349.     if (offset > (10000/MSPTICK) || offset < 0)
  350.         clkval = ntime;
  351.  
  352.     /* Handle possibility of several missed ticks */
  353.     while (ntime != clkval) {
  354.         ++clkval;
  355.         icmpclk();
  356.         tick();
  357.         (void) iss();
  358.     }
  359. }
  360.  
  361.  
  362. getds()
  363. {
  364.     return 0;
  365. }
  366.  
  367.  
  368. audit()
  369. {
  370. }
  371.  
  372.  
  373. doshell(argc, argv)
  374. char    **argv;
  375. {
  376.     int    i, stat, pid, pid1, (*savi)();
  377.     char    *cp, str[MAXCMD], *getenv();
  378.     struct termio tt_config;
  379.  
  380.     str[0] = '\0';
  381.     for (i = 1; i < argc; i++) {
  382.         strcat(str, argv[i]);
  383.         strcat(str, " ");
  384.     }
  385.  
  386.     ioctl(0, TCGETA, &tt_config);
  387.     ioctl(0, TCSETAW, &savecon);
  388.  
  389.     if ((cp = getenv("SHELL")) == NULL || *cp != '\0')
  390.         cp = "/bin/sh";
  391.  
  392.     if ((pid = fork()) == 0) {
  393.         if (argc > 1)
  394.             (void)execl("/bin/sh", "sh", "-c", str, 0);
  395.         else
  396.             (void)execl(cp, cp, (char *)0, (char *)0, 0);
  397.         perror("execl");
  398.         exit(1);
  399.     } else if (pid == -1) {
  400.         perror("fork");
  401.         stat = -1;
  402.     } else {
  403.         savi = signal(SIGINT, SIG_IGN);
  404.         while ((pid1 = wait(&stat)) != pid && pid1 != -1)
  405.             ;
  406.         signal(SIGINT, savi);
  407.     }
  408.  
  409.     ioctl(0, TCSETAW, &tt_config);
  410.  
  411.     return stat;
  412. }
  413.  
  414.  
  415. dodir(argc, argv)
  416. int    argc;
  417. char    **argv;
  418. {
  419.     int    i, stat;
  420.     char    str[MAXCMD];
  421.     struct termio tt_config;
  422.  
  423.     strcpy(str, "ls -l ");
  424.     for (i = 1; i < argc; i++) {
  425.         strcat(str, argv[i]);
  426.         strcat(str, " ");
  427.     }
  428.  
  429.     ioctl(0, TCGETA, &tt_config);
  430.     ioctl(0, TCSETAW, &savecon);
  431.  
  432.     stat = system(str);
  433.  
  434.     ioctl(0, TCSETAW, &tt_config);
  435.  
  436.     return stat;
  437. }
  438.  
  439.  
  440. rename(s1, s2)
  441. char *s1, *s2;
  442. {
  443.     char tmp[MAXCMD];
  444.  
  445.     (void) sprintf(tmp, "mv %s %s", s1, s2);
  446.     (void) system(tmp);
  447. }
  448.  
  449.  
  450. int
  451. docd(argc, argv)
  452. int argc;
  453. char **argv;
  454. {
  455.     char tmp[MAXCMD];
  456.     char *getcwd();
  457.  
  458.     if (argc > 1) {
  459.         if (chdir(argv[1]) == -1) {
  460.             printf("Can't change directory\n");
  461.             return 1;
  462.         }
  463.     }
  464.     if (getcwd(tmp, sizeof(tmp)) != NULL)
  465.         printf("%s\n", tmp);
  466.  
  467.     return 0;
  468. }
  469.  
  470.  
  471. mkdir(s, m)
  472. char    *s;
  473. int    m;
  474. {
  475.     char tmp[MAXCMD];
  476.  
  477.     sprintf(tmp, "mkdir %s", s);
  478.     if (system(tmp))
  479.         return -1;
  480.     if (chmod(s, m) < 0)
  481.         return -1;
  482.  
  483.     return 0;
  484. }
  485.  
  486.  
  487. rmdir(s)
  488. char    *s;
  489. {
  490.     char tmp[MAXCMD];
  491.  
  492.     sprintf(tmp, "rmdir %s", s);
  493.     if (system(tmp))
  494.         return -1;
  495.  
  496.     return 0;
  497. }
  498.  
  499. /*
  500.  * These are here to prevent chars that are already lower or upper
  501.  * case from being turned into gibberish.
  502.  */
  503. char
  504. tolower(c)
  505. char c;
  506. {
  507.     if (isupper(c))
  508.         return(c - 'A' + 'a');
  509.     else
  510.         return(c);
  511. }
  512.  
  513. char
  514. toupper(c)
  515. char c;
  516. {
  517.     if (islower(c))
  518.         return(c - 'a' + 'A');
  519.     else
  520.         return(c);
  521. }
  522.